<template>
  <div class="layui-container fly-marginTop">
    <div class="fly-panel fly-panel-user" pad20>
      <div class="layui-tab layui-tab-brief" lay-filter="user">
        <ul class="layui-tab-title">
          <li>
            <router-link :to="{ name: 'login' }">登入</router-link>
          </li>
          <li class="layui-this">注册</li>
        </ul>
        <div
          class="layui-form layui-tab-content"
          id="LAY_ucm"
          style="padding: 20px 0"
        >
          <div class="layui-tab-item layui-show">
            <div class="layui-form layui-form-pane">
              <validation-observer ref="observer" v-slot="{ validate }">
                <form method="post">
                  <div class="layui-form-item">
                    <label for="L_email" class="layui-form-label">用户名</label>
                    <validation-provider
                      rules="required|email"
                      name="username"
                      v-slot="{ errors }"
                    >
                      <div class="layui-input-inline">
                        <input
                          type="text"
                          name="username"
                          v-model="username"
                          placeholder="请输入您的邮箱"
                          autocomplete="off"
                          class="layui-input"
                        />
                      </div>
                      <div class="layui-form-mid layui-word-aux">
                        将会成为您唯一的登入名
                      </div>
                      <div class="layui-input-inline error" style="margin-left: 110px">
                        {{ errors[0] }}
                      </div>
                    </validation-provider>
                  </div>
                  <div class="layui-form-item">
                  <label for="L_username" class="layui-form-label">昵称</label>
                  <validation-provider
                    rules="required|min:4"
                    name="name"
                    v-slot="{ errors }"
                  >
                    <div class="layui-input-inline">
                      <input
                        type="text"
                        name="name"
                        v-model="name"
                        placeholder="请输入您的昵称"
                        autocomplete="off"
                        class="layui-input"
                      />
                    </div>
                    <div class="layui-input-inline error" style="margin-left: 110px">{{ errors[0] }}</div>
                  </validation-provider>
                </div>
                  <div class="layui-form-item">
                    <label for="L_pass" class="layui-form-label">密码</label>
                    <ValidationProvider
                      rules="required|min:6|max:16"
                      name="password"
                      v-slot="{ errors }"
                      vid="password"
                    >
                      <div class="layui-input-inline">
                        <input
                          type="password"
                          name="password"
                          v-model="password"
                          placeholder="请设置您的密码"
                          autocomplete="off"
                          class="layui-input"
                        />
                      </div>
                      <div class="layui-form-mid layui-word-aux">
                        6到16个字符
                      </div>
                      <div class="layui-input-inline error" style="margin-left: 110px">
                        {{ errors[0] }}
                      </div>
                    </ValidationProvider>
                  </div>
                  <div class="layui-form-item">
                    <label for="L_repass" class="layui-form-label"
                    >确认密码</label
                    >
                    <ValidationProvider
                      rules="required|confirmed:password"
                      name="repass"
                      v-slot="{ errors }"
                    >
                      <div class="layui-input-inline">
                        <input
                          type="password"
                          name="repass"
                          v-model="repass"
                          placeholder="请确认您的密码"
                          autocomplete="off"
                          class="layui-input"
                        />
                      </div>
                      <div class="layui-input-inline error" style="margin-left: 110px">
                        {{ errors[0] }}
                      </div>
                    </ValidationProvider>
                  </div>
                  <div class="layui-form-item">
                    <label for="L_vercode" class="layui-form-label">验证码</label>
                    <validation-provider
                      rules="required|length:4"
                      name="code"
                      v-slot="{ errors }"
                    >
                      <div class="layui-input-inline">
                        <input
                          type="text"
                          name="code"
                          v-model="code"
                          placeholder="请输入验证码"
                          autocomplete="off"
                          class="layui-input"
                        />
                      </div>
                      <div v-html="svg" @click="_getCode()"></div>
                      <div class="layui-input-inline error" style="margin-left: 110px">{{ errors[0] }}</div>
                    </validation-provider>
                  </div>
                  <div class="layui-form-item">
                    <button type="button" class="layui-btn" @click="validate().then(submit)">立即注册</button>
                  </div>
              </form>

              </validation-observer>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getCode, reg } from '@/api/login'
import { ValidationObserver, ValidationProvider } from 'vee-validate'

export default {
  name: 'reg',
  data () {
    return {
      name: '',
      username: '',
      password: '',
      repass: '',
      code: '',
      svg: ''
    }
  },
  components: {
    ValidationProvider,
    ValidationObserver
  },
  mounted () {
    this._getCode()
  },
  methods: {
    _getCode () {
      const sid = this.$store.state.sid
      getCode(sid).then((res) => {
        if (res.code === 200) {
          this.svg = res.data
        }
      })
    },
    async submit () {
      const isValid = await this.$refs.observer.validate()
      if (!isValid) return
      reg({
        name: this.name,
        username: this.username,
        password: this.password,
        code: this.code,
        sid: this.$store.state.sid
      }).then((res) => {
        if (res.code === 200) {
          this.name = ''
          this.username = ''
          this.password = ''
          this.repass = ''
          this.code = ''
          requestAnimationFrame(() => {
            this.$refs.observer.reset()
          })
          // 跳转到登录页面，让用户登录
          this.$alert('注册成功，请登录')
          setTimeout(() => {
            this.$router.push('/login')
          }, 1000)
        } else {
          // res.msg = {username:[],name:[],code:[]}
          this.$refs.observer.setErrors(res.msg)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.error {
  color: rgb(204, 0, 0);
  clear: both;
}
</style>
